// 在需要请求的页面引入
import http from '@/utils/http.js';

// 在方法中使用
export function getSuccessList(param) {
	// GET请求示例
	return http({
		url: '/app-api/member/success-case/page', // 接口路径
		method: 'GET',
		param
	});
}

export function getSuccessDetail(param) {
	// GET请求示例
	return http({
		url: '/app-api/member/success-case/getByCaseId', // 接口路径
		method: 'GET',
		param
	});
}

export function getMissionList(param) {
	// GET请求示例
	return http({
		url: '/app-api/member/task/main/page', // 接口路径
		method: 'GET',
		param
	});
}
export function getMyOrderList(param) {
	// GET请求示例
	return http({
		url: '/app-api/member/task/page', // 接口路径
		method: 'GET',
		param
	});
}
export function getMissionDeatail(param) {
	// GET请求示例
	return http({
		url: '/app-api/member/task/get', // 接口路径
		method: 'GET',
		param
	});
}
export function getMissionComment(param) {
	// GET请求示例
	return http({
		url: '/app-api/member/task-comment/page', // 接口路径
		method: 'GET',
		param
	});
}

export function sendMsgByPhone(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/auth/send-sms-code', // 接口路径
		method: 'POST',
		data
	});
}
export function getUserInfo(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/user/get', // 接口路径
		method: 'GET',
		data
	});
}
export function getPlan(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/task-scheme/page', // 接口路径
		method: 'GET',
		data
	});
}
export function evaluatePlan(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/task-scheme/evaluate', // 接口路径
		method: 'PUT',
		data
	});
}
export function evaluateTask(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/task-comment/create', // 接口路径
		method: 'POST',
		data
	});
}

export function updateMobile(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/user/update-mobile', // 接口路径
		method: 'PUT',
		data
	});
}

export function updateUser(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/user/update', // 接口路径
		method: 'PUT',
		data
	});
}

export function updateEmail(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/user/update-mail', // 接口路径
		method: 'PUT',
		data
	});
}

export function sendMsgByMail(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/auth/send-mail-code', // 接口路径
		method: 'POST',
		data
	});
}

export function updatePassword(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/user/update-password-direct', // 接口路径
		method: 'PUT',
		param: data
	});
}

export function certification(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/user/certification', // 接口路径
		method: 'PUT',
		param: data
	});
}


export function getBizConfig() {
	// GET请求示例
	return http({
		url: '/app-api/infra/config/biz', // 接口路径
		method: 'GET'
	});
}

export function addRewardOrder(data) {
	// GET请求示例
	return http({
		url: '/app-api/member/task/add-reward', // 接口路径
		method: 'POST',
		param: data
	});
}

// 创建支付订单
export function createOrder(taskId) {
	return http({
		url: '/app-api/member/task/order/create',
		method: 'POST',
		param: {
			taskId: taskId
		}
	});
}

// 提现记录查询
export function withdrawRecords(param) {
	return http({
		url: '/app-api/member/brokerage-withdraw/page',
		method: 'GET',
		param: param,
		header: {
			lang: param.lang
		}
	});
}

// 分销记录查询
export function brokerageRecords(param) {
	return http({
		url: '/app-api/member/brokerage-record/page',
		method: 'GET',
		param: {
			status: 1,
			...param
		},
		header: {
			lang: param.lang
		}
	});
}

//申请成为专家
export function applyReceiverUser() {
	return http({
		url: '/app-api/member/brokerage-user/receiver-apply', // 接口路径
		method: 'Put'
	});
}

//申请成为推广员
export function applySpreaderUser() {
	return http({
		url: '/app-api/member/brokerage-user/spreader-apply', // 接口路径
		method: 'Put'
	});
}

export function submitOrder(data) {
	// GET请求示例
	return http({
		url: '/app-api/pay/order/submit', // 接口路径
		method: 'POST',
		data: {
			id: '',
			channelCode: 'wx_lite',
			channelExtras: {},
			...data
		}
	});
}

//申请提现
export function applyWithdraw(data) {
	return http({
		url: '/app-api/member/brokerage-withdraw/create', // 接口路径
		method: 'POST',
		data: {
			type: 5,
			...data
		},
		header: {
			lang: data.lang
		}
	});
}
// 获取微信临时登录凭证 code
export function getWxLoginCode() {
	return new Promise((resolve, reject) => {
		wx.login({
			success: (res) => {
				if (res.code) {
					resolve(res); // 成功获取 code
				} else {
					reject(new Error('获取登录凭证失败'));
				}
			},
			fail: (err) => {
				reject(new Error('微信登录失败: ' + err.errMsg));
			}
		});
	});
}

 // 微信小程序的一键登录
 export function weixinMiniAppLogin(phoneCode, loginCode, state) {
    return http({
      url: '/app-api/member/auth/weixin-mini-app-login',
      method: 'POST',
      data: {
        phoneCode,
        loginCode,
        state
      }
    });
  }

// 预支付
export async function prepay(id) {
	let data = {
		id: id,
		channelCode: 'wx_lite',
		channelExtras: {}
	};
	// 特殊逻辑：微信公众号、小程序支付时，必须传入 openid
	const openid = uni.getStorageSync("openid");
	// 如果获取不到 openid，微信无法发起支付，此时需要引导
	if (!openid) {
		// 跳转到登录页面
		uni.clearStorageSync();
		uni.reLaunch({
			url: '/pages/login/login'
		})
		return;
	}
	data.channelExtras.openid = openid;
	// 发起预支付 API 调用
	const res = await submitOrder(data);

	const payConfig = JSON.parse(res.displayContent);
	return payConfig;
}

// 分销记录查询
export function getAreaTree(param) {
	return http({
		url: '/app-api/system/area/tree',
		method: 'GET',
	});
}

// 专家治疗方案
export function createScheme(data) {
	return http({
		url: '/app-api/member/task-scheme/create',
		method: 'POST',
		data
	});
}
// 专家治疗方案
export function getSurveyList(data) {
	return http({
		url: '/app-api/member/survey-config/list',
		method: 'GET',
		data
	});
}
// 专家治疗方案
export function createMission(data) {
	return http({
		url: '/app-api/member/task/create',
		method: 'POST',
		data
	});
}
